<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="报修单号" prop="repairCode">
        <el-input
          v-model="queryParams.repairCode"
          placeholder="请输入报修单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报修人" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入报修人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="repairStatus">
        <el-select v-model="queryParams.repairStatus" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.repair_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="repairsList">
      <el-table-column label="维修单号" align="center" prop="repairCode"/>
      <el-table-column label="报修人" align="center" prop="name"/>
      <el-table-column label="报修电话" align="center" prop="mobile"/>
      <el-table-column label="报修时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="repairStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.repair_status" :value="scope.row.repairStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="维修人" align="center" prop="sysUser.nickName"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <router-link :to="{path:'/repairs/detail',query:{id:scope.row.id}}">
            <el-button type="text"
                       icon="el-icon-edit" style="margin: 0 10px 5px 0" size="mini">详情
            </el-button>
          </router-link>
          <el-button
            v-if="scope.row.repairStatus===0||scope.row.repairStatus===1"
            size="mini"
            type="text"
            @click="handleReview(scope.row)"
            v-hasPermi="['repairs:repairs:edit']"
          >指派
          </el-button>
          <el-button
            v-if="scope.row.repairStatus===1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleFinish(scope.row)"
            v-hasPermi="['repairs:repairs:finish']"
          >完成
          </el-button>
          <!--<el-button
            v-if=""
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['repairs:repairs:remove']"
          >删除
          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改维修对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item prop="memberId">
          <span slot="label">维修人</span>
          <el-select v-model="form.userId" filterable placeholder="请选择维修人">
            <el-option
              v-for="user in userList"
              :key="user.userId"
              :label="user.nickName"
              :value="user.userId">
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listRepairs, getRepairs, delRepairs, reviewRepairs, finishRepairs} from "@/api/repairs/repairs";
  import {listUser} from "@/api/system/user";

  export default {
    name: "Repairs",
    dicts: ['repair_status'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 维修表格数据
        repairsList: [],
        // 用户表格数据
        userList: null,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          address: null,
          createDate: null,
          details: null,
          imgs: null,
          mobile: null,
          name: null,
          updateDate: null,
          memberId: null,
          repairStatus: null,
          userId: null,
          customerId: null,
          commentRepair: null,
          commentMember: null,
          commentInfo: null,
          repairCode: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
      this.getUserList();
    },
    methods: {
      /** 查询维修列表 */
      getList() {
        this.loading = true;
        listRepairs(this.queryParams).then(response => {
          this.repairsList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 查询用户列表 */
      getUserList() {
        this.loading = true;
        listUser().then(response => {
            this.userList = response.rows;
          }
        );
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          address: null,
          createDate: null,
          details: null,
          imgs: null,
          mobile: null,
          name: null,
          remark: null,
          updateDate: null,
          createBy: null,
          updateBy: null,
          memberId: null,
          repairStatus: null,
          userId: null,
          customerId: null,
          commentRepair: null,
          commentMember: null,
          commentInfo: null,
          repairCode: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 修改按钮操作 */
      handleReview(row) {
        if (row.sysUser && row.sysUser.userId) {
          this.form.userId = row.sysUser.userId;
        } else {
          this.form.userId = null;
        }
        this.ids = row.id;
          this.open = true;
          this.title = "修改维修";
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
              reviewRepairs({
                id:this.ids,
                ...this.form
              }).then(response => {
                this.$modal.msgSuccess("设置成功");
                this.open = false;
                this.getList();
              });
          }
        });
      },
      /** 完成按钮操作 */
      handleFinish(row) {
        const ids = row.id;
        this.$modal.confirm('是否确认完成维修编号为"' + ids + '"的数据项？').then(function () {
          return finishRepairs(row);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("确认完成");
        }).catch(() => {
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id;
        this.$modal.confirm('是否确认删除维修编号为"' + ids + '"的数据项？').then(function () {
          return delRepairs(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('repairs/repairs/export', {
          ...this.queryParams
        }, `repairs_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
